/**
 * jims
 */
package com.jims.master.modules.operation.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.operation.entity.*;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.utils.BloodUtils;
import com.jims.master.modules.utils.OperationStaffUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.operation.dao.OperationScheduleDao;

/**
 * 手术安排Service
 *
 * @author DT
 * @version 2017-04-13
 */
@Service
@Transactional(readOnly = true)
public class OperationScheduleService extends CrudService<OperationScheduleDao, OperationSchedule> {
    @Autowired
    OperationStaffBeforeService operationStaffBeforeService;
    @Autowired
    private OperationScheduleDao operationScheduleDao;
    @Autowired
    private OperationApplyService operationApplyService;
    @Autowired
    private OperationNameBeforeService operationNameBeforeService;
    @Autowired
    private OperationRoomService operationRoomService;
    @Autowired
    private  OperationDictService operationDictService;
    @Autowired
    private OrgDeptService orgDeptService;

    public OperationSchedule get(String id) {
        return super.get(id);
    }

    public List<OperationSchedule> findList(OperationSchedule operationSchedule) {
        return super.findList(operationSchedule);
    }

    public Page<OperationSchedule> findPage(Page<OperationSchedule> page, OperationSchedule operationSchedule) {
        operationSchedule.setOperStatus("1");
        return super.findPage(page, operationSchedule);
    }

    public Page<OperationSchedule> checkPage(Page<OperationSchedule> page, OperationSchedule operationSchedule) {
        return super.findPage(page, operationSchedule);
    }

    public List<OperationSchedule> findByRoomNo(OperationSchedule operationSchedule) {
        return dao.findByRoomNo(operationSchedule);
    }
    @Transactional(readOnly = false)
    public int save(OperationSchedule operationSchedule) {
        return super.save(operationSchedule);
    }
    /**
     * 删除
     * @param operationSchedule
     * @return
     */
    @Transactional(readOnly = false)
    public int deleteAll(OperationSchedule operationSchedule){
        int i=0;
        i=operationScheduleDao.delete(operationSchedule);
        OperationNameBefore operationNameBefore=new OperationNameBefore();
        operationNameBefore.setScheduleId(operationSchedule.getId());
        i=operationNameBeforeService.delete(operationNameBefore);
        OperationStaffBefore operationStaffBefore=new OperationStaffBefore();
        operationStaffBefore.setScheduleId(operationSchedule.getId());
        i=operationStaffBeforeService.delete(operationStaffBefore);
        return 1;
    }
    @Transactional(readOnly = false)
    public int delete(OperationSchedule operationSchedule) {
        return super.delete(operationSchedule);
    }
    /**
     * 手术预约人员获取
     * @param operationStaffBefore
     * @param state
     * @return
     */
    @Transactional(readOnly = false)
    public OperationStaffBefore getStaff(OperationStaffBefore operationStaffBefore,String state){
        operationStaffBefore.setOperationLocal(state);
        operationStaffBefore=operationStaffBeforeService.findIds(operationStaffBefore);
        if(StringUtils.isNotBlank(operationStaffBefore.getUserIds())){
            operationStaffBefore=OperationStaffUtils.getStaffBeforeNames(operationStaffBefore);
        }
        return operationStaffBefore;
    }
    @Transactional(readOnly = false)
    public int saveOperationSchedule(OperationSchedule operationSchedule) {
//        try {
        if(StringUtils.isNotBlank(operationSchedule.getOperatingRoomId())){
            OrgDept orgDept = new OrgDept();
            orgDept.setId(operationSchedule.getOperatingRoomId());
            OrgDept orgDept1=orgDeptService.get(orgDept);
            operationSchedule.setOperatingRoomName(orgDept1.getDeptName());
        }
        if(StringUtils.isNotBlank(operationSchedule.getOperatingRoomNo())){
            OperationRoom operationRoom = new OperationRoom();
            operationRoom.setId(operationSchedule.getOperatingRoomNo());
            OperationRoom operationRoom1=operationRoomService.get(operationRoom);
            operationSchedule.setOperatingNoName(operationRoom1.getRoomNo());
        }
        if(operationSchedule.getOperStatus().equals("0")){//判断是已申请的
            OperationApply operationApply=new OperationApply();
            operationApply.setId(operationSchedule.getId());
            OperationApply operationApply1=operationApplyService.get(operationApply);
            operationApply1.setOperStatus("1");//状态变为已安排
            operationApplyService.save(operationApply1);
            operationSchedule.setOperStatus("1");//状态变为已安排
            operationSchedule.setId(null);
        }
            super.save(operationSchedule);
            /**
             * 保存手术项目
             */
            if (operationSchedule.getOperationNameApplys() != null && operationSchedule.getOperationNameApplys().size() > 0) {

                /*for (OperationNameBefore operationNameBefore : operationSchedule.getOperationNameApplys()) {
                    operationNameBefore.setScheduleId(operationSchedule.getId());
                    OperationDict operationDict=operationDictService.get(operationNameBefore.getOperationId());
                    //String operationName = operationDictService.getNameById(operationNameApply.getOperationId());
                    operationNameBefore.setOperationName(operationDict.getOperationName());
                    operationNameBefore.setOperationCode(operationDict.getOperationCode());
                    operationNameBeforeService.save(operationNameBefore);
                   // operationScheduleDao.itemSave(operationNameApply);

                }*/
                List<OperationNameBefore> operationNameBefores=operationSchedule.getOperationNameBefores();
                List<OperationNameApply> operationNameApplies=operationSchedule.getOperationNameApplys();
                for(int i=0;i<operationNameApplies.size();i++){
                    operationNameBefores.get(i).setOperationId(operationNameApplies.get(i).getOperationId());
                   //operationNameBefores.get(i).set(operationNameApplies.get(i).getOperationCode());
                    OperationDict operationDict=operationDictService.get(operationNameBefores.get(i).getOperationId());
                    operationNameBefores.get(i).setOperationName(operationDict.getOperationName());
                    operationNameBefores.get(i).setOperationCode(operationDict.getOperationCode());
                    operationNameBeforeService.save(operationNameBefores.get(i));
                }
            }
            //先删除再保存
                int j=operationStaffBeforeService.delStaffBefore(operationSchedule.getId());

            /**
             * 保存主刀医生
             */
            if(StringUtils.isNotBlank(operationSchedule.getZdStaffBefore().getUserId())){
                operationSchedule.getZdStaffBefore().setScheduleId(operationSchedule.getId());
                operationStaffBeforeService.save(operationSchedule.getZdStaffBefore());
            }
            /**
             * 保存麻醉医生
             */
            if(StringUtils.isNotBlank(operationSchedule.getMyStaffBefore().getUserId())){
                operationSchedule.getMyStaffBefore().setScheduleId(operationSchedule.getId());
                operationStaffBeforeService.save(operationSchedule.getMyStaffBefore());
            }
            /**
             * 保存医生助手人员
             */
            if(StringUtils.isNotBlank(operationSchedule.getYzStaffBefore().getUserIds())){
                String userIds_z=operationSchedule.getYzStaffBefore().getUserIds();
                String id_z=operationSchedule.getId();
                String local_z=operationSchedule.getYzStaffBefore().getOperationLocal();
                OperationStaffUtils.saveStaffBefore(id_z,local_z,userIds_z);
            }
            /**
             * 保存麻醉助手人员
             */
            if(StringUtils.isNotBlank(operationSchedule.getMzStaffBefore().getUserIds())){
                String userIds_m=operationSchedule.getMzStaffBefore().getUserIds();
                String id_m=operationSchedule.getId();
                String local_m=operationSchedule.getMzStaffBefore().getOperationLocal();
                OperationStaffUtils.saveStaffBefore(id_m,local_m,userIds_m);
            }
            /**
             * 保存输血医生人员
             */
            if(StringUtils.isNotBlank(operationSchedule.getSxStaffBefore().getUserIds())){
                String userIds_s=operationSchedule.getSxStaffBefore().getUserIds();
                String id_s=operationSchedule.getId();
                String local_s=operationSchedule.getSxStaffBefore().getOperationLocal();
                OperationStaffUtils.saveStaffBefore(id_s,local_s,userIds_s);
            }

            //供应护士
            if(StringUtils.isNotBlank(operationSchedule.getGyStaffBefore().getUserIds())){
                String userIds_g=operationSchedule.getGyStaffBefore().getUserIds();
                String id_g=operationSchedule.getId();
                String local_g=operationSchedule.getGyStaffBefore().getOperationLocal();
                OperationStaffUtils.saveStaffBefore(id_g,local_g,userIds_g);
            }

            //上台护士
            if(StringUtils.isNotBlank(operationSchedule.getStStaffBefore().getUserIds())){
                String userIds_t=operationSchedule.getStStaffBefore().getUserIds();
                String id_t=operationSchedule.getId();
                String local_t=operationSchedule.getStStaffBefore().getOperationLocal();
                OperationStaffUtils.saveStaffBefore(id_t,local_t,userIds_t);
            }

            return 1;
        }
//    catch (
//                Exception e
//                )
//        {
//            e.getMessage();
//            return 0;
//        }
//    }
    /**
     * 手术安排打印
     * @param operationSchedule
     * @return
     */
    public String operationSchedulePrint(OperationSchedule operationSchedule){
        User user = UserUtils.getUser();
        String title="手术通知单";
        List<BaseDto> list=dao.operationSchedulePrint(operationSchedule);
        Map map = new HashMap();
        Double totalPrice=0.0;
        if(list.size()>0){
            map.put("warehouseKeeper", BloodUtils.getNameByUserId(list.get(0).getAsString("warehouseKeeper")));
            map.put("purchaseNumber",list.get(0).getAsString("purchaseNumber"));
            int i=1;
            for(BaseDto detail:list){

            }
        }
        String hospital = UserUtils.getUser().getCompany().getOrgName();
        map.put("title",title);
        map.put("totalPrice",String.format("%.2f", totalPrice));
        map.put("hospital",hospital);
        map.put("lists",list);
        String printPath = PrintPdfUtils.previewPdf(user.getId(), "his/operation/operationSchedulePrint.jasper", map, list, "operationSchedulePrint");
        return  printPath;
    }
}